#include "shopdialog.h"
#include "shopdialogitems.h"
#include "gameanimation.h"
#include "gamedialogmanager.h"
#include "gametable.h"
#include "player.h"
#include "ghostboarditems.h"
ShopDialog::ShopDialog(GameDialogManager *m, GameDialogBg *f)
    :GameDialog(m, f)
{
    _bg = new ShopDialogBg(this, QPixmap(":/common/images/shop_bg.jpg"));
    _owner = new ShopDialogOwner(this, QPixmap(":/common/images/shopGirl.png"));
    _text = new ShopDialogText(this);

    ShopDialogButton* _exitButton = new ShopDialogExitButton(this);
    _buttons << _exitButton;


}

void ShopDialog::organizeElements(){
    int total = _bg->boundingRect().height();
    int t_HEIGHT = _text->boundingRect().height();
    _text->moveBy(0, total/2-t_HEIGHT/2-20);
    int totalW = _bg->boundingRect().width();
    int buttonW = ShopDialogButton::WIDTH;
    int buttonH = ShopDialogButton::HEIGHT;
    for(int i =0; i< _buttons.length(); i++)
        _buttons[i]->moveBy(-totalW/2+buttonW/2+20, -total/2+buttonH/2+(20+buttonH)*i+20);

    //animation
    GameParAnimation* bg_anim = new GameParAnimation(this);
    bg_anim->addFade(_bg, 0,1, 300);
    bg_anim->addFade(_text, 0, 1, 300);
    QPointF s = pos();
    s.setX(s.x()+20);
    QPointF e = pos();
    GameParAnimation* owner_anim = new GameParAnimation(this);
    owner_anim->addFade(_owner, 0,1, 300);
    owner_anim->addMove(_owner, s, e, 300);
    for(int i =0; i< _buttons.length(); i++)
        owner_anim->addFade(_buttons[i], 0, 1, 300);

    for(int i =0; i< _buttons.length(); i++){
        s = _buttons[i]->pos();
        e = s;
        s.setX(s.x()-20);
        owner_anim->addMove(_buttons[i], s, e, 300);
    }
    GameSeqAnimation* anim = new GameSeqAnimation(this);
    anim->addAnimation(bg_anim);
    anim->addAnimation(owner_anim);
    showAnim->addAnimation(anim);


    //hide anim
    hideAnim->addFade(_bg, 1, 0, 300);
    hideAnim->addFade(_text, 1, 0, 300);
    hideAnim->addFade(_owner, 1, 0, 300);
    for(int i =0; i< _buttons.length(); i++)
        hideAnim->addFade(_buttons[i], 1, 0, 300);
}

void ShopDialog::prepareStart(){
    _bg->setOpacity(0);
    _owner->setOpacity(0);
    _text->setOpacity(0);
    for(int i =0; i< _buttons.length(); i++)
        _buttons[i]->setOpacity(0);
}

int ShopDialog::execDialog(){
    prepareStart();
    this->startDialog();
    int r = e.exec();

    return r;
}

QRectF ShopDialog::boundingRect() const{
    return QRectF();
}

void ShopDialog::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){

}


//herbalist shop dialog
HerbalistShopDialog::HerbalistShopDialog(GameDialogManager *m, GameDialogBg *f)
    :ShopDialog(m, f)
{
    ShopDialogButton* herbalButton = new ShopDialogHerbalistButton(this);
    _buttons << herbalButton;
    organizeElements();
}

void HerbalistShopDialog::takeTaoItem(){
    int num = 2;
    QList<GameDialogDice*> _dices = getDialogManager()->askDiceRoll(num);
    for(int i = 0; i<_dices.length(); i++){
        switch(_dices[i]->getSideColor()){
        case YELLOW:
            getGameTable()->getCurrentPlayer()->resistChangeY(true);
            break;
        case BLUE:
            getGameTable()->getCurrentPlayer()->resistChangeBlue(true);
            break;
        case RED:
            getGameTable()->getCurrentPlayer()->resistChangeR(true);
            break;
        case WHITE:
            //getResultWindow()->increment(WHITE);
            break;
        case BLACK:
            getGameTable()->getCurrentPlayer()->resistChangeBlack(true);;
            break;
        case GREEN:
            getGameTable()->getCurrentPlayer()->resistChangeG(true);;
            break;
        }
    }
}





//sorcerer shop
SorcererShopDialog::SorcererShopDialog(GameDialogManager *m, GameDialogBg *f)
    :ShopDialog(m, f)
{
    ShopDialogButton* sorcererButton = new ShopDialogSorcererButton(this);
    _buttons << sorcererButton;
    organizeElements();
}

void SorcererShopDialog::discardGhost(){
    //temp hide
    moveToBackEnd();
    GameSlotRequestDialog* ref = getDialogManager()->getSlotRequestDialog();
    CenterTable* cTable = getGameTable()->getCenterTable();
    //temp connect for use, disconnect after finished
    connect(cTable, SIGNAL(clicked(GhostSlot*)), ref, SLOT(receiveClickOnSlot(GhostSlot*)));
    GhostSlot* slot = getDialogManager()->askSlotRequestDialog();
    disconnect(cTable, SIGNAL(clicked(GhostSlot*)), ref, SLOT(receiveClickOnSlot(GhostSlot*)));
    if(slot!=0)
    {
        slot->removeGhostCard();
    }
    //bring it back
    moveToFrontEnd();
}


//pavilion shop
PavilionShopDialog::PavilionShopDialog(GameDialogManager *m, GameDialogBg *f)
    :ShopDialog(m, f)
{
    ShopDialogButton* pavilionButton = new ShopDialogPavilionButton(this);
    _buttons << pavilionButton;
    organizeElements();
}


void PavilionShopDialog::moveGhostAndPlayer(){
    //temp hide
    moveToBackEnd();
    GameSlotRequestDialog* ref = getDialogManager()->getSlotRequestDialog();
    CenterTable* cTable = getGameTable()->getCenterTable();
    //temp connect for use, disconnect after finished
    connect(cTable, SIGNAL(clicked(GhostSlot*)), ref, SLOT(receiveClickOnSlot(GhostSlot*)));
    GhostSlot* from = getDialogManager()->askSlotRequestDialog();
    GhostSlot* to = getDialogManager()->askSlotRequestDialog();
    disconnect(cTable, SIGNAL(clicked(GhostSlot*)), ref, SLOT(receiveClickOnSlot(GhostSlot*)));
    if(from!=0 && to!=0)
    {
        Ghost* g = from->getGhostData();
        from->removeGhostCard();
        to->setGhostCard(g);
    }
    //bring it back
    moveToFrontEnd();
}


//cetemery shop
CemeteryShopDialog::CemeteryShopDialog(GameDialogManager *m, GameDialogBg *f)
    :ShopDialog(m, f)
{
    ShopDialogButton* cemeteryButton = new ShopDialogCemeteryButton(this);
    _buttons << cemeteryButton;
    organizeElements();
}


void CemeteryShopDialog::faceupVillage(){
    moveToBackEnd();

    GameTileRequestDialog* ref = getDialogManager()->getTileRequestDialog();
    CenterTable* cTable = getGameTable()->getCenterTable();
    //temp connect for use, disconnect after finished
    connect(cTable, SIGNAL(clicked(VillageTile*)), ref, SLOT(receiveClickOnTile(VillageTile*)));
    VillageTile* tile = getDialogManager()->askTileDialog();
    disconnect(cTable, SIGNAL(clicked(VillageTile*)), ref, SLOT(receiveClickOnTile(VillageTile*)));
    if(tile!=0)
    {
        //tile recover
    }

    moveToFrontEnd();
}
